
function Promise(executor){
    this.PromiseState = 'pending';
    this.PromiseResult = undefined;
    this.callbacks = [];
    let resolve = (value)=>{
        if(this.PromiseState !== 'pending') return;
        this.PromiseState = 'fulfilled';
        this.PromiseResult = value;
        if(this.callbacks.length){
            this.callbacks.forEach(item=>{
                item.success();
            })
        }
    }
    let reject = (reason)=>{
        if(this.PromiseState !== 'pending') return;
        this.PromiseState = 'rejected';
        this.PromiseResult = reason;
        if(this.callbacks.length){
            this.callbacks.forEach(item=>{
                item.fail();
            })
        }
    }
    try{
        executor(resolve,reject);
    }catch(e){
        reject(e);
    }
}
Promise.prototype.then = function(onResolved, onRejected){
    // 函数内部自己调用自己：递归调用
    return new Promise((resolve,reject)=>{
        if(this.PromiseState === 'fulfilled'){
            let rs
            try{
                rs = onResolved(this.PromiseResult);
            }catch(e){
                reject(e);
                console.log(1111111111);
                return;
            }
           
            // 1. rs是Promise ：你成功我成功，你失败我失败
            // 2. rs不是Promise: fulfilled，rs就是成功的结果值
            if(rs instanceof Promise){
                //rs 是一个 promise
                // rs 什么时候可以判定rs成功了，怎么知道rs又失败了呢？
                // 1. 判断它的状态值，rs.PromiseState === 'fulfilled', 对，但不全对。==》 错
                // 2. rs.then(rs成功的回调，rs失败的回调),rs是成功的promise就一定会执行成功的回调，如果是失败的promise，那就一定会执行失败的回调
                rs.then(v=>{
                    resolve(v);
                },r=>{
                    reject(r)
                })
            }else{
                // 非Promise
                resolve(rs);
            }
        }
        if(this.PromiseState === 'rejected'){
            let rs = onRejected(this.PromiseResult)
            if(rs instanceof Promise){
                // rs.then(v=>{
                //     resolve(v);
                // },r=>{
                //     reject(r);
                // })
                // 这一行等同于上面5行
                rs.then(resolve,reject);
            }else{
                resolve(rs);
            }
        }
        if(this.PromiseState === 'pending'){
            this.callbacks.push({
                success:()=>{
                    let rs = onResolved(this.PromiseResult);
                    if(rs instanceof Promise){
                        rs.then(resolve, reject);
                        // rs.then(v=>{
                        //     resolve(v);
                        // },r=>{
                        //     reject(r);
                        // })
                    }else{
                        resolve(rs);
                    }
                },
                fail:()=>{
                    let rs = onRejected(this.PromiseResult);
                    if(rs instanceof Promise){
                        rs.then(resolve, reject);
                    }else{
                        resolve(rs);
                    }
                }
            })
        }
    }) 
}